<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">

<link rel="stylesheet" href="../../../../common/css/bootstrap-table.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

<script src="../../../../common/js/bootstrap-table.min.js"></script>
<script src="../../../../common/js/bootstrap-table-zh-CN.min.js"></script>

<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>

<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>

<link href="../../../../static/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="../../../../static/css/style.css" rel="stylesheet">
<body>

<table id="tbodyShow">

</table>


<!-- 模态框 -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">入库调度单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <input id="SGDid" type="text" style="display: none">
                <button class='btn btn-primary'  onclick="f()"data-dismiss="modal">登记</button>
            </div>
                <div class="shadow" style="width: 80%;height: 30%;margin-top: 5px;margin-left: 10%;padding: 10px">
                    <div class="text-center">
                        <h2>入库调度单</h2>
                    </div>

                    <div class="form-group row">

                        <label for="gatherId" class="col-sm-2 col-form-label">入库单编号:</label>
                        <div class="col-sm-6">
                            <input class="form-control" readonly type="text" id="gatherId">
                        </div>
                    </div>
                    <div class="form-group row">

                        <label for="reason" class="col-sm-2 col-form-label">入库理由:</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" id="reason">
                        </div>
                        <div class="col-2"></div>
                        <label for="storer" class="col-sm-2 col-form-label">入库详细理由:</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" id="storer">
                        </div>
                    </div>

                    <div>

                        <table class="table">
                            <tbody>
                            <tr style="background-color: gray">
                                <th>产品编号</th>
                                <th>产品名称</th>
                                <th>库房名称</th>
                                <th>存放地址</th>
                                <th>应入库数量</th>
                                <th>已入库数量</th>
                                <th>本次入库数量</th>
                                <th>序列号</th>
                            </tr>
                            </tbody>
                            <tbody id="tbodyShow2">

                            </tbody>
                        </table>

                    </div>

                    <div class="form-group row">

                        <label for="amountSum" class="col-sm-2 col-form-label"> 应入库总件数：</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" id="amountSum">
                        </div>
                        <div class="col-2"></div>
                        <label for="aa" class="col-sm-2 col-form-label"> 已入库总件数：</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" value="0.0" id="aa">
                        </div>

                    </div>

                    <div class="form-group row">
                        <label for="costPriceSum" class="col-sm-2 col-form-label">应入库总金额；</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" id="costPriceSum">
                        </div>
                        <div class="col-2"></div>
                        <label for="bb" class="col-sm-2 col-form-label">已入库库总金额；</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" value="0.0" id="bb">
                        </div>
                    </div>

                    <div class="form-group row">

                        <label for="amountSum" class="col-sm-2 col-form-label" > 登记人：</label>
                        <div class="col-sm-3">
                            <input class="form-control" readonly type="text" id="register">
                        </div>
                        <div class="col-2"></div>
                        <label for="costPriceSum" readonly class="col-sm-2 col-form-label">登记时间；</label>
                        <div class="col-sm-3">
                            <input class="form-control" type="text" id="registerTime">
                        </div>
                    </div>




                </div>

            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>

        </div>
    </div>
</div>

<script>
    $(function () {
        querySGatherPage()
    })
    function querySGatherPage() {
        $("#tbodyShow").bootstrapTable({
            method: "get",
            contentType: "application/json;charset=utf-8",
            url: "/queryStorageXiaotan.action",
            pageNumber: "1",
            pageSize: "3",
            striped: true,
            pagination: true,
            height:600,
            sidePagination: "server",
            pageList: [3, 5, 7, 9],
            queryParams: function (parts) {
                return {
                    currentPage: (parts.offset / parts.limit) + 1,
                    pageSize: parts.limit
                }
            },
            columns: [
                {title: "入库单编号", field: "gatherId"},
                {
                    title: "入库理由", field: "reason", formatter: function (value, row, index) {
                        let but = value == "R001-1" ? "生产入库" : value == "R001-2" ? "库存初始" : value == "R001-3"
                            ? "赠送" : value == "R001-4" ? "内部归还" : "其他归还"
                        return but;
                    }
                },
                {title: "登记时间", field: "registerTime"},
                {title: "总件数", field: "amountSum"},
                {title: "总金额", field: "costPriceSum"},
                {
                    title: "操作", field: "id", formatter: function (value, row, index) {
                        let but = "<button class='btn btn-primary' onclick='queryById(" + value + ")' data-toggle='modal' data-target='#myModal'>登记</button>"
                        return but;
                    }
                },
            ]
        })
    }

        function queryById(data) {
            $.ajax({
                type:"POST",
                url:"/queryBYIDXiaotan.action?id="+data,
                success:function (SGBean) {
                    add(SGBean)
                }
            })
        }


        function add(sG) {

            $("#tbodyShow2").html("")
            $("#gatherId").val(sG.gatherId)
            $("#storer").val(sG.reasonexact)

            $("#reason").val(sG.reason=="R001-1"?"生产入库":sG.reason=="R001-2"?"库存初始":sG.reason=="R001-3"
                ?"赠送":sG.reason=="R001-4"?"内部归还":"其他归还")
            $("#amountSum").val(sG.amountSum)
            $("#costPriceSum").val(sG.costPriceSum)
            $("#checkTime").val(new Date().toLocaleString())

            $("#register").val(sG.register)
            $("#registerTime").val(sG.registerTime)

            for (let i=0;i<sG.list.length;i++){
                dd(sG,i)
            }
            f2(sG.list)
    }
    function dd(data,i) {

        // $("#SGDid").val(data.list[d].id)
        // $("#gatherId2").val(data.gatherId)
        // $("#productId2").val(data.list[d].productId)
        // $("#productName2").val(data.list[d].productName)
        // $("#costPrice2").val(data.list[d].costPrice)
        // $("#register2").val(data.register)
        // $("#registerTime2").val(data.registerTime)
        // $("#amount2").val(data.list[d].amount)
        // $("#subtotal2").val(data.list[d].subtotal)

        $.ajax({
            type:"POST",
            url:"/queryByProductIdKey.action?pid="+data.list[i].productId,
            success:function (data2) {

                $("#tbody3").html("")
                $("#tbodyShow2").append("<tr>" +
                    "<td>" + data.list[i].productId + "</td>" +
                    "<td>" + data.list[i].productName + "</td>" +
                    "<td>成品房</td>" +
                    "<td>" + data2.firstKindName+"-"+data2.secondKindName+"-"+data2.thirdKindName + "</td>" +
                    "<td class='zs'>" + data.list[i].amount + "</td>" +
                    "<td class='yrk'>" + data.list[i].gatheredAmount + "</td>" +
                    "<td><input type='number' class='quantity' onblur='f1()' value='1'></td>" +
                    "<td class='mid'>" + data.list[i].id + "</td>" +
                    "</tr>")

            }
        })

    }
    function f1() {
        for (let i=0;i<$(".zs").length;i++){
            let zs=parseInt($(".zs:eq("+i+")").text());
            let yrk=parseInt($(".yrk:eq("+i+")").text());
            let h=zs-yrk;
            let quantity=$(".quantity:eq("+i+")").val();
            if(quantity>h){
                alert("当前数量不足")
                $(".quantity:eq("+i+")").val(0)
            }

        }

    }
    function f() {
        let length = $(".mid").length;
        let arr=[];
        for (let i=0;i<length;i++){
            let bean={
                id:$(".mid:eq("+i+")").text(),
                quantityOfThisTime:$(".quantity:eq("+i+")").val()
            }
            arr.unshift(bean)
        }
        let listSD={
            gatherId:$("#gatherId").val(),
            list:arr
        }
        $.ajax({
            type:"POST",
            contentType:"application/json;charset=utf-8",
            url:"/updateSGatherDetailsQuantityOfThisTime.action",
            data:JSON.stringify(listSD),
            success:function () {
                alert("登记成功,请等待审核")
                window.location.reload()
            }
        })
    }
    function f2(list) {

        console.log(list)
       let zjs=0;
       let zje=0;
        for (let i=0;i<list.length;i++){
           zje+=parseInt(list[i].gatheredAmount)*parseInt(list[i].costPrice)
            zjs+=list[i].gatheredAmount
        }
        $("#aa").val(zjs)
        $("#bb").val(zje)
    }
</script>
</body>
</html>